package collection3;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

/**
 * HashMap集合的使用
 * 存储结构：哈希表（数组+链表+红黑树）
 * 使用key的hashcode（）和equals（）
 *
 */
public class Demo02 {
    public static void main(String[] args) {
       //创建集合  刚创建好hashmap 之后没有添加元素 table = null size =  0 节省空间
        HashMap<Student ,String> students = new  HashMap<Student ,String>();
        //添加元素
        Student s1 =  new Student("curry",100);
        Student s2 =  new Student("James",101);
        Student s3 =  new Student("kobe",102);
        Student s4 =  new Student("zff",103);
        students.put(s1,"金州");
        students.put(s2,"Los");
        students.put(s3,"Los");
        students.put(s4,"厦门");
        students.put(new Student("curry",100),"金州");//若想要去重，则重写hashcode()和equals()
        System.out.println("元素个数：" + students.size());
        System.out.println(students.toString());
        //删除元素
//        students.remove(s4);
//        students.remove(s2,"Los");
        //3.1 使用keySet()方法遍历元素
        System.out.println("========= 使用keySet()方法遍历元素========");
        for (Student key : students.keySet()) {
            System.out.println(key +  students.get(key));
        }
        //3.2使用entrySet()方法遍历元素
        System.out.println("======== 使用entrySet()方法遍历元素========");
        for (Map.Entry<Student, String> entry : students.entrySet()) {
            System.out.println(entry.getKey() + entry.getValue());
        }
        //4.判断
        System.out.println(students.containsKey(s1));
        System.out.println(students.containsValue("北京"));

    }
}
